package com.koron.order.risk.base.excel;

import cn.hutool.core.map.BiMap;
import cn.hutool.core.util.StrUtil;
import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.excel.converters.AutoConverter;
import com.alibaba.excel.metadata.GlobalConfiguration;
import com.alibaba.excel.metadata.data.ReadCellData;
import com.alibaba.excel.metadata.data.WriteCellData;
import com.alibaba.excel.metadata.property.ExcelContentProperty;
import com.koron.common.core.business.datadict.utils.DataDictTool;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * @author jzp
 * @since 2023/1/16
 */
public class RiskAutoConvertor extends AutoConverter {
    /**
     * {
     *     字段名：{
     *         数据库值：前端显示值
     *     }
     * }
     */
    private final Map<String, BiMap<String, String>> fieldMap = new HashMap<>();
    private final String title = "$title$";

    public RiskAutoConvertor(){
        // 危险等级 riskLevel
        BiMap<String, String> riskLevelMap = new BiMap<>(DataDictTool.getDataDictByCode("transmissionRiskControl", "risk_danger_level"));
        riskLevelMap.putAll(new BiMap<>(DataDictTool.getDataDictByCode("transmissionRiskControl", "risk_level")));
        riskLevelMap.put(title, "危险等级");

        BiMap<String, String> yesOrNoMap = new BiMap<>(new HashMap<>());
        yesOrNoMap.put("0", "否");
        yesOrNoMap.put("1", "是");
        yesOrNoMap.put(title, "是或否");

        BiMap<String, String> isProvideMap = new BiMap<>(new HashMap<>());
        isProvideMap.put("0", "未提供");
        isProvideMap.put("1", "已提供");
        isProvideMap.put(title, "是否提供");

        BiMap<String, String> isSignMap = new BiMap<>(new HashMap<>());
        isSignMap.put("0", "未签署");
        isSignMap.put("1", "已签署");
        isSignMap.put(title, "是否签署");

        BiMap<String, String> keyLevelMap = new BiMap<>(new HashMap<>());
        keyLevelMap.put("1", "I");
        keyLevelMap.put("2", "II");
        keyLevelMap.put(title, "关键级");

        // 字段和数据字典对应
        fieldMap.put("riskLevel", riskLevelMap);
        fieldMap.put("combustibleRoof", yesOrNoMap);
        fieldMap.put("lineProtectWorked", yesOrNoMap);
        fieldMap.put("letterApplied", yesOrNoMap);
        fieldMap.put("riskNotification", yesOrNoMap);
        fieldMap.put("certificateDept", isProvideMap);
        fieldMap.put("commitmentLetter", isProvideMap);
        fieldMap.put("certificatePerson", isProvideMap);
        fieldMap.put("securityProtocol", isSignMap);
    }

    @Override
    public Class<?> supportJavaTypeKey() {
        return Object.class;
    }

    @Override
    public Object convertToJavaData(ReadCellData<?> cellData, ExcelContentProperty contentProperty,
                                    GlobalConfiguration globalConfiguration) {
        String key = cellData.getStringValue();
        if (contentProperty == null) {
            return key;
        }
        ExcelProperty excelProperty = contentProperty.getField().getAnnotation(ExcelProperty.class);
        if (excelProperty == null || excelProperty.converter() == AutoConverter.class) {
            return key;
        }
        BiMap<String, String> entries = fieldMap.get(contentProperty.getField().getName());
        if (entries != null) {
            key = entries.getKey(cellData.getStringValue());
            if (StrUtil.isNotEmpty(cellData.getStringValue()) && StrUtil.isEmpty(key)) {
                return cellData.getStringValue();
            }
        }
        Class<?> type = contentProperty.getField().getType();
        if (StrUtil.isEmpty(key)) {
            return null;
        }
        if (type == Integer.class) {
            return Integer.parseInt(key);
        }
        return key;
    }

    @Override
    public WriteCellData<String> convertToExcelData(Object value, ExcelContentProperty contentProperty,
                                               GlobalConfiguration globalConfiguration) {
        if (contentProperty == null) {
            return new WriteCellData<>(Objects.toString(value, ""));
        }
        ExcelProperty excelProperty = contentProperty.getField().getAnnotation(ExcelProperty.class);
        if (excelProperty.converter() != null && excelProperty.converter() != AutoConverter.class) {
            BiMap<String, String> entries = fieldMap.get(contentProperty.getField().getName());
            if (entries != null) {
                return new WriteCellData<>(entries.getOrDefault(Objects.toString(value, ""), ""));
            }
        }
        return new WriteCellData<>(Objects.toString(value, ""));
    }

}
